package com.lenovo.lcdm.dcm.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.lenovo.lcdm.common.enums.CommonMsgEnum;
import com.lenovo.lcdm.common.exception.ServiceException;

import java.util.Objects;
import java.util.function.Supplier;

/**
 * @author
 */
public class Checker {


    public static <T> T notNull(T t, CommonMsgEnum CommonMsgEnum, Object... CommonMsgEnumArgs) {
        if (Objects.isNull(t)) {
            throw new ServiceException(CommonMsgEnum, CommonMsgEnumArgs);
        }

        return t;
    }

    public static <T> T notNull(T t, Supplier<ServiceException> error) {
        if (Objects.isNull(t)) {
            throw error.get();
        }

        return t;
    }

    public static void isTrue(boolean isTrue, CommonMsgEnum CommonMsgEnum, Object... CommonMsgEnumArgs) {
        if (!isTrue) {
            throw new ServiceException(CommonMsgEnum, CommonMsgEnumArgs);
        }
    }

    public static void isTrue(boolean isTrue, Supplier<ServiceException> error) {
        if (!isTrue) {
            throw error.get();
        }
    }


    public static <T> T notEmpty(T t, CommonMsgEnum CommonMsgEnum, Object... CommonMsgEnumArgs) {
        if (ObjectUtil.isEmpty(t)) {
            throw new ServiceException(CommonMsgEnum, CommonMsgEnumArgs);
        }

        return t;
    }

    public static <T> T notEmpty(T t, Supplier<ServiceException> error) {
        if (ObjectUtil.isEmpty(t)) {
            throw error.get();
        }

        return t;
    }

    public static <T> void equals(T t1, T t2, CommonMsgEnum CommonMsgEnum, Object... CommonMsgEnumArgs) {
        if (!Objects.equals(t1, t2)) {
            throw new ServiceException(CommonMsgEnum, CommonMsgEnumArgs);
        }
    }

    public static <T> void equals(T t1, T t2, CommonMsgEnum CommonMsgEnum) {
        if (!Objects.equals(t1, t2)) {
            throw new ServiceException(CommonMsgEnum);
        }
    }
}
